package test

import java.util
import java.util.Timer

sealed trait List[+A]
case object Nil extends List[Nothing]
case class Cons[+A](head: A, tail: List[A]) extends List[A]

object List{

	def sum(ints: List[Int]): Int = ints match {
		case Nil => 0
		case Cons(x, xs) => x + sum(xs)
	}

	def product(ds: List[Double]): Double = ds match {
		case Nil => 1.0
		case Cons(0.0, _) => 0.0
		case Cons(x, xs) => x * product(xs)
	}

	def apply[A](as: A*): List[A] = {
		if(as.isEmpty) Nil
		else Cons(as.head, apply(as.tail: _*))
	}

	def tail[A](as: List[A]) = as match {
		case Cons(_, xs) => xs
	}

	def setHead[A](as: List[A],a: A) = as match {
		case Nil => List(a)
		case Cons(_, xs) => Cons(a, xs)
	}

	def drop[A](l: List[A], n: Int): List[A] = {
		if(n <= 0) l
		else l match {
			case Nil => Nil
			case Cons(_, t) => drop(t, n - 1)
		}
	}

	def dropWhile[A](l: List[A], f: A => Boolean): List[A] = l match {
		case Cons(h, t) if(f(h)) => dropWhile(t, f)
		case _ => l
	}

	def init[A](l: List[A]): List[A] = l match {
		case Nil => sys.error("empty list")
		case Cons(_, Nil) => Nil
		case Cons(head, tail) => Cons(head, init(tail))
	}

	def main(args: Array[String]): Unit = {
		println(init(List("a", "b", "c")))
	}
}

